package cn.zhj.mvpart.mvp

import android.content.ContentValues
import android.util.Log
import cn.zhj.mvpart.integration.EventBusManager
import kotlinx.coroutines.CoroutineExceptionHandler

abstract class BasePresenter<M:IModel , V:IView> : IPresenter{
    val mModel: M by lazy {createModel()}
    var mRootView: V? = null

    abstract fun createModel(): M


    /**
     * 在基类里，提供协程出现错误的调用
     * 回调在子线程里
     */
    val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
        Log.e("thisError-presenter", "coroutine: error ${throwable.message}")
        Log.e("thisError-presenter", "请求结束 异常了 线程id：${Thread.currentThread().id}")
    }

    open fun useEventBus(): Boolean {
        return true
    }

    override fun onCreate(view:IView) {
        mRootView = view as V
        if(useEventBus()){
            EventBusManager.register(this)
        }
    }

    override fun onDestroy() {
        if(useEventBus()){
            EventBusManager.unRegister(this)
        }
        mModel?.onDestroy()
        mRootView = null

    }


}